var paginationTotal = 5;//记录总数
var isSearch = false;
var browseType = 'fold';    //浏览<{$lang.type}>
var isReload=false;
function search(val){
	if(val){
		$('#shipping_method_sch').val(val);
	}
	isSearch = true;
	paginationCurrentPage=1;
	initData(0);
}
    
function loadData(page, pageSize) {
    var vForm = {};
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serialize();
        vForm+='&browsetype='+browseType;
    }
    vForm.browsetype = browseType;
    loading();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/order/waitshipment/list/page/" + page + "/pageSize/" + pageSize,
        data: vForm,
        success: function(json) {
            closeLoading();
            var list = "";
            var trclass="";
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='9' class=\"center\">&nbsp;<{$lang.no_data}></td>";
            } else {
                //var i = page==1 ? 1 : pageSize * (page-1)+1;
                $.each(json.result, function(key, val) {
                    if ((key + 1) % 2 == 1 ) {
                        trclass='';
                    } else {
                        trclass='even-tr';
                    }
                    if(browseType!='fold'){trclass='';}
                    list += "<tr class="+trclass+">";
                    list += "<td><input type='checkbox' value='" + val.orders_code + "' id='orderids_" + val.orders_code + "' name='orderids[]'></td>";
                    list += "<td><a href=\"javascript:detail('" + val.orders_code + "')\">" + val.orders_code + "</a></td>";
                    list += "<td>" + val.customer_id + "</td>";
                    list += "<td>" + val.consignee_country_id + "</td>";
                    list += "<td>" + val.shipping_method + "</td>";
//                    list += "<td>" + val.parcel_value + "</td>";
                    list += "<td>" + val.add_time + "</td>";
                    list += "<td>" + val.orders_status + "</td>";
//                    list += "<td>" + val.osh_type + "</td>";
                    list += "</tr>";
                    if (val.order_product != null && val.order_product !='') {
                        list += "<tr><td colspan='9' style='padding:0;'>";
                        $.each(val.order_product, function(key, value) {
                            list += "<table width=100% border=0 cellspacing=0 cellpadding=0><tr class='even-tr'>";
                            list+="<td width='8%'>&nbsp;</td>";
//                            list += "<td width='20%'><a href=\"javascript:proDetails('" + value.op_id + "')\">" + value.product_id + "</a></td>";
                            list += "<td width='20%'>" + value.product_id + "</td>";
                             list += "<td width='12%'><{$lang.price}>:" + value.op_unit_price + "</td>";
                            list += "<td width='15%'><{$lang.quantity}>:" + value.op_quantity + "</td>";
                            list += "<td width='15%'><{$lang.ws_code}>:" + value.ws_code + "</td>";
                            list += "<td width='15%'><{$lang.print}><{$lang.time}>:</td>";
                            list += "<td width='15%'><{$lang.shipping}><{$lang.time}>:</td>";
                            list += "</tr></table>";
                        });
                        list += "</td></tr>";
                    }
                });
            }
            $("#loadData").html(list);
        }
    });
}

function voResult(){
	var lwh=true;
	var length,width,height;
	if(!isNum($("#txt_length").val())){
		lwh=false;
	}else{
		length=$("#txt_length").val();
	}
	if(!isNum($("#txt_width").val())){
		lwh=false;
	}else{
		width=$("#txt_width").val();
	}
	if(!isNum($("#txt_height").val())){
		lwh=false;
	}else{
		height=$("#txt_height").val();
	}
	if(lwh){
		var voresult=parseFloat(length*width*height);
		if(parseFloat($("#volWeightRate").val())>0){
			voresult=voresult/$("#volWeightRate").val();
		}
		$("#voresult").html((voresult).toFixed(2));
		if(voresult>$("#os_weight").val()){
		    $("#voresult").css("color","red");
		}else{
		    $("#voresult").css("color","blue");
		}
	   
	    //color:#0033FF;
	}else{
		$("#voresult").html(0);
	}
	
}

$(function() {
//    initData(0);
    $("[name=ckAll]").click(function() {
        if ($(this).is(':checked')) {
            $("[name='orderids[]']").attr('checked', true);
        } else {
            $("[name='orderids[]']").attr('checked', false);
        }
    });

    $('#order_form').dialog({
        autoOpen: false,
        width: 800,
        modal: true,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                //submitOrder();
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            $('#orderForm')[0].reset();
        }
    });

   
    
    $("#txt_length").keyup(function(){
    	voResult();
    });
    $("#txt_width").keyup(function(){
    	voResult();
    });
    $("#txt_height").keyup(function(){
    	voResult();
    });
    
    $("#os_weight").keyup(function(){
    	voResult();
    });
    

	var arrTip = new Array('<{$lang.length}>', '<{$lang.width}>','<{$lang.height}>');
    $("#txt_length").blur(function() {
		var text = $("#txt_length").html();
		if (this.value == "") {
			this.style.color = '#b6b7b9';
			this.value = arrTip[0];
		}
		voResult();
	});
    
    
	$("#txt_length").focus(function() {
		var text = $("#txt_length").val();
		if (text == arrTip[0]) {
		   this.style.color = '#404040';
		   this.value="";
		}
		
	});
	$("#txt_width").blur(function() {
		var text = $("#txt_width").html();
		if (this.value == "") {
			this.style.color = '#b6b7b9';
			this.value = arrTip[1];
		}
		voResult();
	});
	$("#txt_width").focus(function() {
		var text = $("#txt_width").val();
		if (text == arrTip[1]) {
		   this.style.color = '#404040';
		   this.value="";
		}
		
	});
	$("#txt_height").blur(function() {
		var text = $("#txt_height").html();
		if (this.value == "") {
			this.style.color = '#b6b7b9';
			this.value = arrTip[2];
		}
		voResult();
	});
	$("#txt_height").focus(function() {
		var text = $("#txt_height").val();
		if (text == arrTip[2]) {
		   this.style.color = '#404040';
		   this.value="";
		}
		
	});
	$("#txt_length").attr("disabled",true);
	$("#txt_width").attr("disabled",true);
	$("#txt_height").attr("disabled",true);	
	
});

//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}



//<{$lang.order}><{$lang.detail}>
function detail(orders_code) {
    var url = '/order/order/detail/orders_code/' + orders_code;
    window.open(url);
}
//浏览风格
function swichBrowseType(vSelf) {
    if (browseType == 'fold') {
        browseType = 'unfold';
        $(vSelf).val('<{$lang.unfolder}><{$lang.product}>');
    } else {
        browseType = 'fold';
        $(vSelf).val('<{$lang.folder}><{$lang.product}>');
    }
    initData(paginationCurrentPage - 1, paginationPageSize);
}
//<{$lang.product}><{$lang.detail}>

function proDetails(id) {
    if (isNaN(id) || !id) {
        return false;
    }
    var url = '/order/order/prodetail/id/' + id;
    window.parent.$('<iframe id="ifr_order_product_detail" name="ifr_order_product_detail" src="' + url + '" />').dialog({
        title: '<{$lang.order}><{$lang.product}><{$lang.detail}><{$lang.msg}>',
        autoOpen: true,
        width: 900,
        modal: false,
        resizable: false,
        position:'top',
        autoResize: true,
        close:function() {
            $(this).remove();
            if (isReload) {
                window.location.reload();
                isReload = false;
            }
        },
        buttons: {
            'Close/<{$lang.close}>': function() {
                $(this).remove();
            }
        }
    }).width(875);
}



//<{$lang.operation}><{$lang.tip}>
function crmTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
        	 '!':function(){
        		 //$(this).dialog("close");
        	    },
            '<{$lang.ok}>': function() {
               ignoreWeightSubmit();
			   $(this).dialog("close");
			    $("#txt_length").attr("disabled",true);
				$("#txt_width").attr("disabled",true);
				$("#txt_height").attr("disabled",true);
				$("#txt_length").val("<{$lang.length}>");
				$("#txt_width").val("<{$lang.width}>");
				$("#txt_height").val("<{$lang.height}>");
				$("#voresult").html(0);
            },
			'<{$lang.close}>': function() {
                $(this).dialog("close");
            }
        }
    });
}

//忽略<{$lang.weight}>进行<{$lang.submit}>
function ignoreWeightSubmit(){
   vForm = "";
    vForm = $("#waitshipmentForm").serializeArray();
	var orders_shipment=$.trim($("#orders_shipment").val());
	var os_weight=$.trim($("#os_weight").val());
	if(orders_shipment.length==0){
	   alert("<{$lang.pls_input}><{$lang.orders_code}>!");
	   $("orders_shipment").focus();
	   return;
	}
    if($("#tracking_number").val()==''){
        alert("<{$lang.pls_input}><{$lang.shipping_code}>!");
        $("#tracking_number").focus();
        return;
    }
	if(os_weight.length==0){
	   //alert("<{$lang.weight}><{$lang.can_not_be_empty}>");
	   $("os_weight").focus();
	   return;
	}
	
	
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/order/waitshipment/ignoreweightupdate",
        data: vForm,
        success: function(json) {
            if (json.ask==1) {
			    var err='';
				$.each(json.error,function(k,v){
					err+=v;
				});
				$("#shipmode").html(json.shipmode);
                //alertTip(err);
                //initData(0);
				$("#msgshow").html(err);
				$("#txt_length").attr("disabled",true);
				$("#txt_width").attr("disabled",true);
				$("#txt_height").attr("disabled",true);
				$("#txt_length").val("<{$lang.length}>");
				$("#txt_width").val("<{$lang.width}>");
				$("#txt_height").val("<{$lang.height}>");
				$("#voresult").html(0);
				$("#volWeightRate").val(0);
				$("#orders_shipment").val("");
				$("#os_weight").val("");
				$("#orders_shipment").select();
                $("#tracking_number").val("");
				//<{$lang.current}>统计
				var temp_smcount=$('#temp_'+json.shipmode);
				if(temp_smcount.length==0){
				   var temp_smc='<label style=\"float:left;width:100px;padding-left:10px;white-space:nowrap;\">'+json.shipmode+'(<span style=\"display:inline-block;color:#0000FF; font-size:14px;\" id=\"temp_'+json.shipmode+'\">1</span>)</label>';
				   $("#temp_allspc").append(temp_smc);
				}else{
				   $('#temp_'+json.shipmode).html(parseInt($('#temp_'+json.shipmode).html())+1);
				}
				$("#temp_shipCount").html(parseInt($("#temp_shipCount").html())+1);
				
				//当天统计
				var smcount=$('#'+json.shipmode);
				if(smcount.length==0){
				   var smc='<label style=\"float:left;width:100px;padding-left:10px;white-space:nowrap;\">'+json.shipmode+'(<span style=\"display:inline-block;color:#0000FF; font-size:14px;\" id=\"'+json.shipmode+'\">1</span>)</label>';
				   $("#allspc").append(smc);
				}else{
				   $('#'+json.shipmode).html(parseInt($('#'+json.shipmode).html())+1);
				}
				
				$("#shipCount").html(parseInt($("#shipCount").html())+1);
				
				return;

            }else{
			    var err='';
				$.each(json.error,function(k,v){
					err+=v;
				});
                alertTip(err);
				return;
			}
        }
    });
}

function isNum(s)
    {
      //判断有效数字的正则表达式
       var pattern=/^[1-9]\d*(.\d+)*$|^0.\d*[1-9]+\d*$/;
        if(pattern.test(s))
        {
           return true;
        }
        else
        {
            return false;        
        }        
    }

function checkweight(){
   var os_weight=$.trim($("#os_weight").val());
   if(os_weight.length==0){
	   alert("<{$lang.weight}><{$lang.can_not_be_empty}>");
	   $("#os_weight").focus();
	   return;	   
	}else{
	   if(!isNum(os_weight)){
		   alert("<{$lang.weight}><{$lang.must_more_zhan_zero}>!");
		   $("#os_weight").select();
		   return;
		}
	}
   
   if($("#txt_length").attr('disabled')=="disabled"){
	   //$(".button").focus();
	   postsubmit();
   }else{
	   $("#txt_length").select();
	  // checkfromweight();
   }

}

function checklength(){
	var get_length='';
	if($("#txt_length").val()=="<{$lang.length}>"){
	   get_length="";
	}else{
	   get_length=$("#txt_length").val();
	   
	   if(!isNum(get_length)){
		   alert("<{$lang.length}><{$lang.must_more_zhan_zero}>");
		   $("#txt_length").select();
		   return;
		}
	   
	   $("#txt_width").select();
	   return;
	}
}

function checkwidth(){
    var get_width='';
	if($("#txt_width").val()=="<{$lang.width}>"){
	   get_width="";
	}else{
	   get_width=$("#txt_width").val();
	   
	   if(!isNum(get_width)){
		   alert("<{$lang.width}><{$lang.must_more_zhan_zero}>");
		   $("#txt_width").select();
		   return;
		}
	   
	   
	   $("#txt_height").select();
	   return;
	}
}

function checkheight(){
	var get_height='';
	if($("#txt_height").val()=="<{$lang.height}>"){
	   get_height="";
	}else{
	   get_height=$("#txt_height").val();
	   
	   if(!isNum(get_height)){
		   alert("<{$lang.height}><{$lang.must_more_zhan_zero}>");
		   $("#txt_height").select();
		   return;
		}
	   
	   postsubmit();
	   //$(".button").focus();
	   return;
	}
}

function checkorder(){
    vForm = "";
	$("#msgshow").html("");
    vForm = $("#waitshipmentForm").serializeArray();
	var orders_shipment=$.trim($("#orders_shipment").val());
	var os_weight=$.trim($("#os_weight").val());
	if(orders_shipment.length==0){
		$("#txt_length").attr("disabled",true);
		$("#txt_width").attr("disabled",true);
		$("#txt_height").attr("disabled",true);
		$("#txt_length").val("<{$lang.length}>");
		$("#txt_width").val("<{$lang.width}>");
		$("#txt_height").val("<{$lang.height}>");
		$("#volWeightRate").val(0);
		alert("<{$lang.pls_input}><{$lang.orders_code}>");
		$("orders_shipment").select();
	   
	   $("#orders_shipment").focus();
	   return;
	}
    if($("#tracking_number").val()==''){
        alert("<{$lang.pls_input}><{$lang.shipping_code}>!");
        $("#tracking_number").select();
        $("#tracking_number").focus();
        return;
    }
	var os_weight=$.trim($("#os_weight").val());
	 if(os_weight.length==0){
		   $("#os_weight").focus();   
	 }else{
		   $("#os_weight").select();
	 }
	

    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/order/waitshipment/update",
        data: vForm,
        success: function(json) {
		   $("#shipmode").html(json.shipmode);
		   if (json.ask==-1){
               alertTip(json.error);
                $("#txt_length").attr("disabled",true);
				$("#txt_width").attr("disabled",true);
				$("#txt_height").attr("disabled",true);
				$("#txt_length").val("<{$lang.length}>");
				$("#txt_width").val("<{$lang.width}>");
				$("#txt_height").val("<{$lang.height}>");
				$("#voresult").html(0);
				$("#volWeightRate").val(0);
                $("#orders_shipment").select();
				return;
		   }
		   
		   
		   if(json.volWeight){
					$("#txt_length").attr("disabled",false);
					$("#txt_width").attr("disabled",false);
					$("#txt_height").attr("disabled",false);
					$("#volWeightRate").val(json.volWeightRate);
				    $("#os_weight").select();
								
			    }else{
				    $("#txt_length").attr("disabled",true);
					$("#txt_width").attr("disabled",true);
					$("#txt_height").attr("disabled",true);
					$("#txt_length").val("<{$lang.length}>");
					$("#txt_width").val("<{$lang.width}>");
					$("#txt_height").val("<{$lang.height}>");
					$("#voresult").html(0);
					$("#volWeightRate").val(0);
					$("#os_weight").select();
		        	return;
				}
        }
    });

}	

//<{$lang.submit}>
function postsubmit() {
    vForm = "";
	$("#msgshow").html("");
    vForm = $("#waitshipmentForm").serializeArray();
	var orders_shipment=$.trim($("#orders_shipment").val());
	var os_weight=$.trim($("#os_weight").val());
	if(orders_shipment.length==0){
	    $("#txt_length").attr("disabled",true);
		$("#txt_width").attr("disabled",true);
		$("#txt_height").attr("disabled",true);
		$("#txt_length").val("<{$lang.length}>");
		$("#txt_width").val("<{$lang.width}>");
		$("#txt_height").val("<{$lang.height}>");
		$("#volWeightRate").val(0);
	   alert("<{$lang.pls_input}><{$lang.orders_code}>");
	   $("orders_shipment").select();
	   return;
	}
	
	   if(os_weight.length==0){
		   alert("<{$lang.weight}><{$lang.can_not_be_empty}>");
		   $("#os_weight").focus();
		   return;	   
		}else{
		   if(!isNum(os_weight)){
			   alert("<{$lang.weight}><{$lang.must_more_zhan_zero}>");
			   $("#os_weight").select();
			   return;
			}
	}
	
	var get_length='';
	if($("#txt_length").val()=="<{$lang.length}>"){
	   get_length="";
	}else{
	   get_length=$.trim($("#txt_length").val());
	   if(!isNum(get_length)){
		   alert("<{$lang.length}><{$lang.must_more_zhan_zero}>");
		   $("#txt_length").select();
		   return;
		}
	}
	var get_width='';
	if($("#txt_width").val()=="<{$lang.width}>"){
	   get_width="";
	}else{
	   get_width=$.trim($("#txt_width").val());
	   if(!isNum(get_width)){
		   alert("<{$lang.width}><{$lang.must_more_zhan_zero}>");
		   $("#txt_width").select();
		   return;
		}
	}
	var get_height='';
	if($("#txt_height").val()=="<{$lang.height}>"){
	   get_height="";
	}else{
	   get_height=$.trim($("#txt_height").val());
	   if(!isNum(get_height)){
		   alert("<{$lang.height}><{$lang.must_more_zhan_zero}>");
		   $("#txt_height").select();
		   return;
		}
	}
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/order/waitshipment/update",
        data: vForm,
        success: function(json) {
		   $("#shipmode").html(json.shipmode);
		   
		   
		   if(json.volWeight){
					$("#txt_length").attr("disabled",false);
					$("#txt_width").attr("disabled",false);
					$("#txt_height").attr("disabled",false);
					$("#volWeightRate").val(json.volWeightRate);
					if(os_weight.length==0){
					       alert("<{$lang.weight}><{$lang.can_not_be_empty}>");
					       $("#os_weight").focus();
					       return;	   
				        }	
					
                    if(get_length.length==0){
					    alert("<{$lang.pls_input}><{$lang.length}>");
						$("#txt_length").focus();
						return;
					}else{
					    if(!isNum(get_length)){
						   alert("<{$lang.length}><{$lang.must_more_zhan_zero}>");
						   $("#txt_length").focus();
						   return;
						}
					}
                    if(get_width.length==0){
					    alert("<{$lang.pls_input}><{$lang.width}>");
						$("#txt_width").focus();
						return;
					}else{
					    if(!isNum(get_width)){
						   alert("<{$lang.width}><{$lang.must_more_zhan_zero}>");
						   $("#txt_width").focus();
						   return;
						}
					}
                    if(get_height.length==0){
					    alert("<{$lang.pls_input}><{$lang.height}>");
						$("#txt_height").focus();
						return;
					}else{
					     if(!isNum(get_height)){
						   alert("<{$lang.height}><{$lang.must_more_zhan_zero}>");
						   $("#txt_height").focus();
						   return;
						}
					}
                   				
			    }else{
				    $("#txt_length").attr("disabled",true);
					$("#txt_width").attr("disabled",true);
					$("#txt_height").attr("disabled",true);
					$("#txt_length").val("<{$lang.length}>");
					$("#txt_width").val("<{$lang.width}>");
					$("#txt_height").val("<{$lang.height}>");
					$("#volWeightRate").val(0);
					if(os_weight.length==0){
				       alert("<{$lang.weight}><{$lang.can_not_be_empty}>");
				       $("#os_weight").select();
				       return;	   
			        }
				}

		    if (json.ask==1){
			    
			    var err='';
				$.each(json.error,function(k,v){
					err+=v;
				});
				$("#shipmode").html(json.shipmode);
                //alertTip(err);
				//initData(0);
				$("#msgshow").html(err);
				
				$("#txt_length").attr("disabled",true);
				$("#txt_width").attr("disabled",true);
				$("#txt_height").attr("disabled",true);
				$("#txt_length").val("<{$lang.length}>");
				$("#txt_width").val("<{$lang.width}>");
				$("#txt_height").val("<{$lang.height}>");
				$("#voresult").html(0);
				$("#volWeightRate").val(0);
                $("#tracking_number").val("");
				$("#orders_shipment").val("");
				$("#os_weight").val("");
				$("#orders_shipment").select();
				
				//<{$lang.current}>统计
				var temp_smcount=$('#temp_'+json.shipmode);
				if(temp_smcount.length==0){
				   var temp_smc='<label style=\"float:left;width:100px;padding-left:10px;white-space:nowrap;\">'+json.shipmode+'(<span style=\"display:inline-block;color:#0000FF; font-size:14px;\" id=\"temp_'+json.shipmode+'\">1</span>)</label>';
				   $("#temp_allspc").append(temp_smc);
				}else{
				   $('#temp_'+json.shipmode).html(parseInt($('#temp_'+json.shipmode).html())+1);
				}
				$("#temp_shipCount").html(parseInt($("#temp_shipCount").html())+1);
				
				//当天统计
				var smcount=$('#'+json.shipmode);
				if(smcount.length==0){
				   var smc='<label style=\"float:left;width:100px;padding-left:10px;white-space:nowrap;\">'+json.shipmode+'(<span style=\"display:inline-block;color:#0000FF; font-size:14px;\" id=\"'+json.shipmode+'\">1</span>)</label>';
				   $("#allspc").append(smc);
				}else{
				   $('#'+json.shipmode).html(parseInt($('#'+json.shipmode).html())+1);
				}
				
				$("#shipCount").html(parseInt($("#shipCount").html())+1);
				
                return;				
			}else if(json.ask==2){
			    $("#shipmode").html(json.shipmode);
				//实际称量重量和预期重量差别大于0.5KG,是否<{$lang.confirm}>继续!
			    crmTip('<{$lang.continue_ship}>'); 
			}else{
			    
                var err='';
                if(json.error){
                	$.each(json.error,function(k,v){
    					err+=v;
    				});
                }
				
                alertTip(err);
                $("#orders_shipment").select();
				return;
            }
        }
    });
}